let privacyHandler
let privacyResolves = new Set()
let closeOtherPagePopUpHooks = new Set()

if (wx.onNeedPrivacyAuthorization) {
  wx.onNeedPrivacyAuthorization(resolve => {
    console.log('触发 onNeedPrivacyAuthorization')
    if (typeof privacyHandler === 'function') {
      privacyHandler(resolve)
    }
  })
}

const closeOtherPagePopUp = (closePopUp) => {
  closeOtherPagePopUpHooks.forEach(hook => {
    if (closePopUp !== hook) {
      hook()
    }
  })
}
Component({
  properties: {
    footFlag:{
      type:Boolean,
      value:false
    },
    firstFlag:{
      type:Boolean,
      value:false
    }
  },
  data: {
      title: "全友定制+",
      desc1: "在您使用【全友定制+】服务之前，请你仔细阅读",
      urlTitle: "《全友定制+用户隐私保护指引》",
      desc2: "，如您同意《全友定制+用户隐私保护指引》，请点击‘同意’开始使用【全友定制+】。",
      innerShow: false,
      height: 0,
  },
  observers:{
    'firstFlag':function(val){
      // console.log(val)
      if(val){
        if (wx.getPrivacySetting) {
          wx.getPrivacySetting({
            success: res => {
                console.log("是否需要授权：", res.needAuthorization, "隐私协议的名称为：", res.privacyContractName)
                if (res.needAuthorization) {
                  this.popUp()
                } else{
                  this.triggerEvent("agree")
                }
            },
            fail: () => { },
            complete: () => { },
          })
        } else {
          // 低版本基础库不支持 wx.getPrivacySetting 接口，隐私接口可以直接调用
          this.triggerEvent("agree")
        }
      }
    }
  },
  lifetimes: {
    // attached: function() {
    //   console.log(this.data.firstFlag)
    //      if(this.data.firstFlag){
    //       if (wx.getPrivacySetting) {
    //         wx.getPrivacySetting({
    //           success: res => {
    //               console.log("是否需要授权：", res.needAuthorization, "隐私协议的名称为：", res.privacyContractName)
    //               if (res.needAuthorization) {
    //                 this.popUp()
    //               } else{
    //                 this.triggerEvent("agree")
    //               }
    //           },
    //           fail: () => { },
    //           complete: () => { },
    //         })
    //       } else {
    //         // 低版本基础库不支持 wx.getPrivacySetting 接口，隐私接口可以直接调用
    //         this.triggerEvent("agree")
    //       }
    //   }
    // },
    attached: function() {
      const closePopUp = () => {
        this.disPopUp()
      }
      privacyHandler = resolve => {
        privacyResolves.add(resolve)
        this.popUp()
        // 额外逻辑：当前页面的隐私弹窗弹起的时候，关掉其他页面的隐私弹窗
        closeOtherPagePopUp(closePopUp)
      }
      
      this.closePopUp = closePopUp
      closeOtherPagePopUpHooks.add(this.closePopUp)
    },
    detached: function() {
      closeOtherPagePopUpHooks.delete(this.closePopUp)
    }
  },
  pageLifetimes: {
    show() {
      if (this.closePopUp) {
        privacyHandler = resolve => {
          privacyResolves.add(resolve)
          this.popUp()
          // 额外逻辑：当前页面的隐私弹窗弹起的时候，关掉其他页面的隐私弹窗
          closeOtherPagePopUp(this.closePopUp)
        }
      }
    }
  },
  methods: {
      handleDisagree(e) {
        this.disPopUp()
        privacyResolves.forEach(resolve => {
          resolve({
            event: 'disagree',
          })
        })
        privacyResolves.clear()
          // this.triggerEvent("disAgree")
          // this.disPopUp()
      },
      handleAgree(e) {
        this.disPopUp()
        // 这里演示了同时调用多个wx隐私接口时要如何处理：让隐私弹窗保持单例，点击一次同意按钮即可让所有pending中的wx隐私接口继续执行 （看page/index/index中的 wx.getClipboardData 和 wx.startCompass）
        privacyResolves.forEach(resolve => {
          resolve({
            event: 'agree',
            buttonId: 'agree-btn'
          })
        })
        privacyResolves.clear()
          // this.triggerEvent("agree")
          // this.disPopUp()
      },
      popUp() {
        if (this.data.innerShow === false) {
          this.setData({
            innerShow: true
          })
        }
      },
      disPopUp() {
        if (this.data.innerShow === true) {
          this.setData({
            innerShow: false
          })
        }
      },
      openPrivacyContract() {
        wx.openPrivacyContract({
          success: res => {
            console.log('openPrivacyContract success')
          },
          fail: res => {
            console.error('openPrivacyContract fail', res)
          }
        })
      }
  }
})